package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.bean.DeviceBean;
import com.mercenary.wein.screenControl.deviceControll.AndroidDevice;
import com.mercenary.wein.screenControl.ui.GroupComboBox;
import com.mercenary.wein.screenControl.ui.JPanelScreen;
import com.mercenary.wein.screenControl.ui.inter.GroupCheckBoxOb;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by tuxiao on 2017/3/19.
 */
public class GroupCheckBoxPanel extends JPanel {
    Log log = LogFactory.getLog(GroupCheckBoxPanel.class);
    GroupCheckBox groupCheckBox;
    GroupComboBox comboBox;


    public GroupCheckBoxPanel(){
        super();
        setPreferredSize(new Dimension(180, 550));//设置大小
        setBackground(Color.WHITE);
        Border titleBorder1 = BorderFactory.createTitledBorder("请选择在线设备");// 设置面板边框，实现分组框的效果       .设置面板边框颜色
        setBorder(titleBorder1);
        //checkbox部分
        groupCheckBox = new GroupCheckBox();
        groupCheckBox.setLayout(new GridLayout(30,1));
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;//
        JScrollPane jsp = new JScrollPane(groupCheckBox, v, h);
        jsp.setPreferredSize(new Dimension(180, 350));
        add(jsp);

        JPanel cbp = new JPanel();
        cbp.setPreferredSize(new Dimension(350, 150));
        //checkbox
        JPanel cbPanel = new JPanel();
        cbPanel.setPreferredSize(new Dimension(450, 40));//设置大小
        comboBox = new GroupComboBox();
        comboBox.registerObserver(groupCheckBox);
        comboBox.notifyGroupChange(comboBox.getSelectedGid());
        cbPanel.add(comboBox);
        cbp.add(cbPanel);
        //button
        JPanel buttonP = new JPanel();
        JButton B1 = new JButton("全选");
        JButton B2 = new JButton("反选");
        JButton B3 = new JButton("清除");
        buttonP.setPreferredSize(new Dimension(180, 30));
        buttonP.setLayout(new GridLayout(1, 3));
        buttonP.add(B1);
        buttonP.add(B2);
        buttonP.add(B3);
        cbp.add(buttonP);
        this.add(cbp);

        //全选按钮的点击事件
        B1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                groupCheckBox.selectAll();
            }

        });
        //反选按钮的点击事件
        B2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                groupCheckBox.invertSelect();
            }
        });
        //清除按钮的点击事件
        B3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                groupCheckBox.unSelect();
            }
        });

        this.updateUI();
    }

    public void registerObserver(GroupCheckBoxOb ob){
        this.groupCheckBox.registerObserver(ob);
    }

    public java.util.List<DeviceBean> getSelectedDevices(){
        log.info("getSelectedDevices");
        log.info(getDNames(groupCheckBox.getChoosedDevices()));
        return groupCheckBox.getChoosedDevices();
    }

    public java.util.List<AndroidDevice> getSelectedADevices(){
        log.info("getSelectedADevices");
        return groupCheckBox.getChoosedADevices();
    }

    private String getDNames(java.util.List<DeviceBean> deviceBeens){
        StringBuilder sb = new StringBuilder("=================================");
        for(DeviceBean d:deviceBeens){
            sb.append(d.getDname());
        }
        return sb.append("==================================").toString();
    }

}
